package com.company.project.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import com.company.project.common.utils.DataResult;
import com.company.project.common.utils.EnumAreaType;
import com.company.project.entity.MatchProjectEntity;
import com.company.project.entity.SysUser;
import com.company.project.service.MatchProjectService;
import com.company.project.vo.resp.ProjectWithAreaDTO;

/**
 * 项目-团队表
 *
 * @author ls
 * @email *****@mail.com
 * @date 2021-02-02 20:34:19
 */
@Controller
@RequestMapping("/")
@Api(tags = "比赛项目模块")
public class MatchProjectController extends BaseController{
    @Autowired
    private MatchProjectService matchProjectService;

    /**
    * 跳转到页面
    */
    @GetMapping("/index/matchProject")
    public String matchProject() {
        return "matchproject/list";
    }
    
    /**
     * 跳转到页面
     */
     @GetMapping("/index/matchProjectLastDay")
     public String matchProjectLastDay() {
         return "matchproject/listLastDay";
     }

    @ApiOperation(value = "新增")
    @PostMapping("matchProject/add")
    @RequiresPermissions("matchProject:add")
    @ResponseBody
    public DataResult add(@RequestBody MatchProjectEntity matchProject){
        matchProjectService.save(matchProject);
        return DataResult.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("matchProject/delete")
    @RequiresPermissions("matchProject:delete")
    @ResponseBody
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids){
        matchProjectService.removeByIds(ids);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("matchProject/update")
    @RequiresPermissions("matchProject:update")
    @ResponseBody
    public DataResult update(@RequestBody MatchProjectEntity matchProject){
        matchProjectService.updateById(matchProject);
        return DataResult.success();
    }

    @SuppressWarnings({"rawtypes" })
	@ApiOperation(value = "查询分页数据")
    @PostMapping("matchProject/listByPage")
    @RequiresPermissions("matchProject:list")
    @ResponseBody
    public DataResult findListByPage(@RequestBody MatchProjectEntity matchProject){
    	SysUser currentUser = getCurrentUser();		
		String username = currentUser.getUsername();		
		String area = EnumAreaType.getIndexByCode(username);		
		Page page = new Page(matchProject.getPage(), matchProject.getLimit());               
        IPage<ProjectWithAreaDTO> result = matchProjectService.getList(page,matchProject.getStartTime(),matchProject.getEndTime(),area,matchProject.getKey());
        return DataResult.success(result);
    }
    
	@SuppressWarnings({"rawtypes" })
	@ApiOperation(value = "查询分页数据昨日项目")
	@PostMapping("matchProject/listByPageLastDay")
	@RequiresPermissions("matchProject:listLastDay")
	@ResponseBody
	public DataResult findListByPageLastDay(@RequestBody MatchProjectEntity matchProject){
		SysUser currentUser = getCurrentUser();		
		String username = currentUser.getUsername();		
		String area = EnumAreaType.getIndexByCode(username);
		Page page = new Page(matchProject.getPage(), matchProject.getLimit());   	    
	    Calendar calendar = Calendar.getInstance();//此时打印它获取的是系统当前时间         
        calendar.add(Calendar.DATE, -1);    //得到前一天
        String  yestedayDate = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
        String start = yestedayDate+" 00:00:00";
        String end = yestedayDate+" 23:59:59";		
        IPage<ProjectWithAreaDTO> result = matchProjectService.getListLastDay(page, start, end, area,matchProject.getKey());
	    return DataResult.success(result);
	}

}
